package cn.hncu.booksManagementSystem.in.dao.impl;

import java.util.ArrayList;
import java.util.Collection;

import cn.hncu.booksManagementSystem.common.enums.XxxIdEnum;
import cn.hncu.booksManagementSystem.in.dao.dao.InDetailDAO;
import cn.hncu.booksManagementSystem.in.vo.InDetailModel;
import cn.hncu.booksManagementSystem.in.vo.InDetailQueryModel;
import cn.hncu.booksManagementSystem.utils.FileIoUtil;
import cn.hncu.booksManagementSystem.utils.GeneratorXxxID_Util;
/**
 * CreateTime: 2018年4月20日 下午11:10:23	
 * @author 宋进宇  Email:447441478@qq.com
 * Description:
 *	InDetailDAO实现类
 */
public class InDetailDAO_FileImpl implements InDetailDAO{
	private final String FILE_NAME = "./data/inDetail.info";
	
	@Override
	public boolean add(InDetailModel inDetail) {
		if (inDetail==null) {
			return false;
		}
		Collection<InDetailModel> col = FileIoUtil.readFromDataBase(FILE_NAME);
		for (InDetailModel model : col) {
			if (model.equals(inDetail)) {
				return false;
			}
		}
		boolean boo = col.add(inDetail);
		boolean boo2 = FileIoUtil.write2DataBase(col, FILE_NAME);
		if (boo&&boo2) {
			GeneratorXxxID_Util.updateXxxId(XxxIdEnum.IN_DETAIL);
			return true;
		}
		return false;
	}

	@Override
	public boolean delete(String id) {
		if (id==null) {
			return false;
		}
		Collection<InDetailModel> col = FileIoUtil.readFromDataBase(FILE_NAME);
		for (InDetailModel model : col) {
			if (model.getId().equals(id)) {
				return col.remove(model)&&FileIoUtil.write2DataBase(col, FILE_NAME);
			}
		}
		return false;
	}

	@Override
	public boolean update(InDetailModel inDetail) {
		if (inDetail==null) {
			return false;
		}
		Collection<InDetailModel> col = FileIoUtil.readFromDataBase(FILE_NAME);
		ArrayList<InDetailModel> list = (ArrayList<InDetailModel>) col;
		for (int i = 0; i < list.size(); i++) {
			if(list.get(i).equals(inDetail)) {
				return list.set(i, inDetail)!=null&&FileIoUtil.write2DataBase(list, FILE_NAME);
			}
		}
		return false;
	}

	@Override
	public InDetailModel getInDetailInfoById(String id) {
		if (id==null) {
			return null;
		}
		Collection<InDetailModel> col = FileIoUtil.readFromDataBase(FILE_NAME);
		for (InDetailModel model : col) {
			if (model.getId().equals(id)) {
				return model;
			}
		}
		return null;
	}

	@Override
	public Collection<InDetailModel> getAll() {
		return FileIoUtil.readFromDataBase(FILE_NAME);
	}

	@Override
	public Collection<InDetailModel> getInDetailsByCondition(InDetailQueryModel idqm) {
		Collection<InDetailModel> col = getAll();
		//如果查询条件为null，直接返回所有记录
		if (idqm==null) {
			return col;
		}
		Collection<InDetailModel> resCol = new ArrayList<InDetailModel>();
		for (InDetailModel model : col) {
			//卫条件1 查id
			//判断查询条件是否有效
			if(idqm.getId()!=null &&!"".equals(idqm.getId())) {
				//不相等跳过
				if (!model.getId().equals(idqm.getId())) {
					continue;
				}
			}
			//卫条件2 查进货单id
			//判断查询条件是否有效
			if(idqm.getInId()!=null &&!"".equals(idqm.getInId())) {
				//不相等跳过
				if (!model.getInId().equals(idqm.getInId())) {
					continue;
				}
			}
			//卫条件3 查进图书id
			//判断查询条件是否有效
			if(idqm.getBookId()!=null &&!"".equals(idqm.getBookId())) {
				//不相等跳过
				if (!model.getBookId().equals(idqm.getBookId())) {
					continue;
				}
			}
			//卫条件4 查数量 范围查询
			//判断查询条件是否有效
			if (idqm.getSumNum()>0) {
				//小于小的跳过
				if (model.getSumNum() < idqm.getSumNum()) {
					continue;
				}
			}
			//判断查询条件是否有效
			if (idqm.getSumNum2()>0) {
				//大于大的跳过
				if (model.getSumNum() > idqm.getSumNum2()) {
					continue;
				}
			}
			//卫条件4 查金额范围查询
			//判断查询条件是否有效
			if (idqm.getSumMoney()>0) {
				//小于小的跳过
				if (model.getSumMoney() < idqm.getSumMoney()) {
					continue;
				}
			}
			//判断查询条件是否有效
			if (idqm.getSumMoney2()>0) {
				//大于大的跳过
				if (model.getSumMoney() > idqm.getSumMoney2()) {
					continue;
				}
			}
			//能到这了说明该记录符合条件
			resCol.add(model);
		}
		return resCol;
	}

}
